<?php

namespace app\admin\controller;

use app\admin\service\GoodsAttrService;
use support\spark\model\goods\GoodsAttrTpl;
use support\Request;
use support\View;

class GoodsAttrController extends Curd
{


    public  function  index(){
        if (request()->isAjax()) {
            $goodsAttrService = new GoodsAttrService();
            $res = $goodsAttrService->getList(request()->all());
            return sparkSuccess($res);
        }
        return view('goods_attr/index');
    }

    /**
     * 添加
     */
    public function add( )
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $goodsAttrService = new GoodsAttrService();
            $res = $goodsAttrService->addGoodsAttr($param);
            return sparkSuccess($res);
        }
        return view('goods_attr/add');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (request()->isAjax()) {
            $param = request()->post();
            $goodsAttrService = new GoodsAttrService();
            $res = $goodsAttrService->editGoodsAttr($param);
            return sparkSuccess($res);
        }
        $id = request()->input('id');
        $goodsAttrTplModel = new GoodsAttrTpl();
        $info = $goodsAttrTplModel->getInfoByWhere([
            'id' => $id
        ]);
        View::assign([
            'info' => $info,
            'attr' => json_decode($info['value'], true)
        ]);

        return view('goods_attr/edit');
    }

    /**
     * 删除
     */
    public function del()
    {
        $id = request()->input('id');
        $goodsAttrTplModel = new GoodsAttrTpl();
        $goodsAttrTplModel->delById($id);
        return sparkSuccess(dataReturn(0,'删除成功'));
    }

}